package com.edgar.download.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


/**
 * Created by dell on 2015/9/24.
 */
public class DownloadDataBaseHelp extends SQLiteOpenHelper implements Downloads.DownloadImpl,Downloads.CourseDownloadImpl {
    private static final String TAG = DownloadDataBaseHelp.class.getSimpleName();
    private static final String DATABASE_NAME = "download.db";
    private static final int DB_VERSION = 10;
    private SQLiteDatabase mSqlDataBase;
    private static DownloadDataBaseHelp sInstance;

    public static DownloadDataBaseHelp getInstance(Context context){
        if(sInstance == null){
            synchronized (DownloadDataBaseHelp.class){
                if(sInstance == null){
                    sInstance = new DownloadDataBaseHelp(context);
                }
            }
        }
        return sInstance;
    }

    private DownloadDataBaseHelp(Context context) {
        super(context, DATABASE_NAME,null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //database first use call oncreate
        createDownloadTable(db);
        createCourseDownloadTable(db);
    }

    private void createDownloadTable(SQLiteDatabase db){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("CREATE TABLE ")
                .append(TAB_NAME)
                .append("(")
                .append(_ID+" integer primary key AutoIncrement,")
                .append(CONTENT_ID +" integer,")
                .append(NAME+" text,")
                .append(SAVE_PATH +" text,")
                .append(DOWNLOAD_URL+" text,")
                .append(STATUS+" integer,")
                .append(PROGRESS + " integer,")
                .append(SIZE + " integer,")
                .append(CURRENT_SIZE + " integer,")
                .append(CONTENT_TYPE + " text,")
                .append(ERROR_STATUS + " integer,")
                .append(ERROR_MESSAGE + " text,")
                .append(DATE + " text)");
        String sql = sqlBuilder.toString();
        db.execSQL(sql);
    }

    private void createCourseDownloadTable(SQLiteDatabase db){
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("CREATE TABLE ")
                .append(COURSE_TAB_NAME)
                .append("(")
                .append(_ID+" integer primary key AutoIncrement,")
                .append(COURSE_ID+" integer,")
                .append(COURSE_NAME+" text,")
                .append(CHAPTER_ID+" integer,")
                .append(CHAPTER_NAME+" text)");
        String sql = sqlBuilder.toString();
        db.execSQL(sql);
    }

    public synchronized SQLiteDatabase getDataBase(){
       if(mSqlDataBase == null){
           mSqlDataBase = getWritableDatabase();
       }
        return mSqlDataBase;
    }

    public synchronized void close(){
        if(mSqlDataBase != null){
            mSqlDataBase.close();
        }
        mSqlDataBase = null;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
